package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask;

import android.content.Context;
import android.text.TextUtils;

import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababybeans.DababyASRequestPms;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyhttp.DababyASReqstManger;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababylogger.DababyASLger;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyservices.DababyASBtryIfo;
import com.test.dababy.dababy_00_path.Paths_dababy;
import com.test.dababy.dababy_00_utils.DababyUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class DababyASDevcBtyTsk implements Supplier<String> {

    private final static String TAG = "ASDeviceBatteryTask";

    private final String mTaskId;
    private final Context mContext;


    public String nIK5="";
    public char vVsO='>';
    public boolean vL7F=true;
    public float jjOi=0.7592146f;
    public char nCvb='7';

    public void q4rQ1(){
        new Thread(() -> {
            try {
                setnIK5(String.valueOf(vVsO)+vL7F+jjOi+nCvb);
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setnIK5(String nIK5) {
        this.nIK5 = nIK5;
    }

    public void setvVsO(char vVsO) {
        this.vVsO = vVsO;
    }

    public void setvL7F(boolean vL7F) {
        this.vL7F = vL7F;
    }

    public void setJjOi(float jjOi) {
        this.jjOi = jjOi;
    }

    public void setnCvb(char nCvb) {
        this.nCvb = nCvb;
    }

    public DababyASDevcBtyTsk(Context context, String taskId) {
        this.mContext = context;
        this.mTaskId = taskId;
    }

    @Override
    public String get() {
        // 日志标记
        DababyASLger.d_dababy(TAG, TAG + " 执行中...");

        // 过滤条件
        if (TextUtils.isEmpty(Paths_dababy.PATH_BATTERY)) {
            return mTaskId + "";
        }

        // 创建任务
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CountDownLatch latch = new CountDownLatch(1);
        AsyncBtyTsk_dababy asyncTask = new AsyncBtyTsk_dababy(mContext, latch, mTaskId);
        executorService.submit(asyncTask);

        // 等待任务完成
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return mTaskId + "";
    }

    static class AsyncBtyTsk_dababy implements Runnable {
        private final CountDownLatch latch;
        private final Context mContext;
        private final String mTaskId;

        AsyncBtyTsk_dababy(Context context, CountDownLatch latch, String taskId) {
            this.mContext = context;
            this.latch = latch;
            this.mTaskId = taskId;
        }

        @Override
        public void run() {
            DababyASBtryIfo batteryInfo = new DababyASBtryIfo();
            batteryInfo.getBatteryInfo_dababy(mContext, batteryInfoBean -> {
                DababyASRequestPms netParams = new DababyASRequestPms();
                netParams.setDBG_dababy(batteryInfoBean);
//                String result = new Gson().toJson(netParams).trim();
                String result = netParams.toEncryptParams_dababy(Paths_dababy.PATH_BATTERY);

                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                    String url = DababyUtil.getCommonPath_dababy();
                    new DababyASReqstManger.Builder_dababy().setReqId_dababy(this.mTaskId).setUrl_dababy(url).setBody_dababy(result).build_dababy().doPostRqst_dababy();

                    return mTaskId + "";
                });

                future.thenRun(latch::countDown);
            });

        }
    }

    public String getmTaskId() {
        return mTaskId;
    }

    public Context getmContext() {
        q4rQ1();
        return mContext;
    }

}
